package gui_xmlparser;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
*
* Rivera, Valencia, De Grano, Tan, Macatangay
*/
public class Exporter 
{
	private ArrayList<Constituent> ConstList;
	private Document doc;
	private String fileSrc = "";
	
	public Exporter(ArrayList<Constituent> ConstList, String filepath)
	{
		fileSrc = filepath;
		this.ConstList = ConstList;
		try 
		{
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			doc = docBuilder.newDocument();
		} 
		catch (ParserConfigurationException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void export()
	{
		try 
		{ 
			Element root = doc.createElement("InfectedEye");
			doc.appendChild(root);
			
			if(!ConstList.isEmpty()){
				for(int i = 0 ;i<ConstList.size();i++)
				{
					Element tempConst = doc.createElement("Constituent");
					this.appendChildren(tempConst,ConstList.get(i));
					root.appendChild(tempConst);
				}
				
				TransformerFactory transformerFactory = TransformerFactory.newInstance();
				Transformer transformer = transformerFactory.newTransformer();
				transformer.setOutputProperty(OutputKeys.INDENT, "yes");
				transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
				DOMSource source = new DOMSource(doc);
				StreamResult result = new StreamResult(new File(fileSrc));
		 
				transformer.transform(source, result);
			}
 
		}
		catch (TransformerException tfe) 
		{
			tfe.printStackTrace();
		}
	}
	
	public void appendChildren(Element e, Constituent c)
	{
		if(c.getLabel()!=null)
		{
			Element tempLabel = doc.createElement("label");
			tempLabel.appendChild(doc.createTextNode(c.getLabel()));
			e.appendChild(tempLabel);
		}
		
		if(c.getFeatures()!=null)
		{
			Element tempFeatures = doc.createElement("Features");
		
			for(Feature f:c.getFeatures().getFeatureList())
			{
				Element tempName = doc.createElement("Name");
				tempName.appendChild(doc.createTextNode(f.getName()));
				tempFeatures.appendChild(tempName);
			
				Element tempValue = doc.createElement("Value");
				tempValue.appendChild(doc.createTextNode(f.getValue()));
				tempFeatures.appendChild(tempValue);
			}
			
			e.appendChild(tempFeatures);
		}
		
		if(c.getConcept()!=null)
		{
			Element tempConcept = doc.createElement("Concept");
			tempConcept.appendChild(doc.createTextNode(c.getConcept()));
			e.appendChild(tempConcept);
		}
		if(c.getSubconstituents()!=null)
		{
			
			for(Constituent subC:c.getSubconstituents().getConstList())
			{
				Element tempSub = doc.createElement("subconstituent");
				this.appendChildren(tempSub, subC);
				e.appendChild(tempSub);
			}
		}
		
	}
}
